package cn.iocoder.yudao.module.social.controller.admin.container;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.module.social.controller.admin.container.vo.*;
import cn.iocoder.yudao.module.social.convert.container.ContainerConvert;
import cn.iocoder.yudao.module.social.dal.dataobject.container.ContainerDO;
import cn.iocoder.yudao.module.social.service.container.ContainerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 容器")
@RestController
@RequestMapping("/social/container")
@Validated
public class ContainerController {

    @Resource
    private ContainerService containerService;

    @PostMapping("/create")
    @Operation(summary = "创建容器")
    @PreAuthorize("@ss.hasPermission('social:container:create')")
    public CommonResult<Long> createContainer(@Valid @RequestBody ContainerCreateReqVO createReqVO) {
        return success(containerService.createContainer(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新容器")
    @PreAuthorize("@ss.hasPermission('social:container:update')")
    public CommonResult<Boolean> updateContainer(@Valid @RequestBody ContainerUpdateReqVO updateReqVO) {
        containerService.updateContainer(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除容器")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('social:container:delete')")
    public CommonResult<Boolean> deleteContainer(@RequestParam("id") Long id) {
        containerService.deleteContainer(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得容器")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('social:container:query')")
    public CommonResult<ContainerRespVO> getContainer(@RequestParam("id") Long id) {
        ContainerDO container = containerService.getContainer(id);
        return success(ContainerConvert.INSTANCE.convert(container));
    }

    @GetMapping("/page")
    @Operation(summary = "获得容器分页")
    @PreAuthorize("@ss.hasPermission('social:container:query')")
    public CommonResult<PageResult<ContainerRespVO>> getContainerPage(@Valid ContainerPageReqVO pageReqVO) {
        PageResult<ContainerDO> pageResult = containerService.getContainerPage(pageReqVO);
        return success(ContainerConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出容器 Excel")
    @PreAuthorize("@ss.hasPermission('social:container:export')")
    public void exportContainerExcel(@Valid ContainerPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ContainerDO> list = containerService.getContainerPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "容器.xls", "数据", ContainerRespVO.class,
                        ContainerConvert.INSTANCE.convertList(list));
    }

}
